﻿using System;
using System.IO;
using System.Web.Mvc;

namespace ShopB2C.Helper {
    /// <summary>
    /// 动态产生缩图图片，原图片路径 ViewData["PhotoPath"] = "指定全路径"
    /// ，ViewData["PhotoWidth"] = 120 为图片宽
    /// ，ViewData["PhotoHeight"] = 120 为图片高
    /// ，路径必须提供，宽高默认为 120
    /// </summary>
    public class ThumbPhotoResult : ActionResult {
        public override void ExecuteResult(ControllerContext context) {
            var response = context.HttpContext.Response;
            ViewDataDictionary vdd = context.Controller.ViewData;
            string photoPath;
            int photoWidth,photoHeight;
            if (vdd.ContainsKey("PhotoPath")) {
                photoPath = vdd["PhotoPath"].ToString();
            }
            else {
                throw new Exception("原图片全路径是必须提供的参数!");
            }
            photoWidth = vdd.ContainsKey("PhotoWidth") ? (int)vdd["PhotoWidth"] : 120;
            photoHeight = vdd.ContainsKey("PhotoHeight") ? (int)vdd["PhotoHeight"] : 120;
            MemoryStream ms = ThumbImageHelper.CuteThumbPhoto(photoWidth, photoHeight,
                photoPath, false, false);
            response.ContentType = Methods.GetContentTypeByExtentsion(Path.GetExtension(photoPath));
            response.BinaryWrite(ms.ToArray());
        }
    }
}
